/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve.util;

import ortustve.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.lang.String;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Collections;

/**
 *
 * @author Aaron
 */
public class array extends ortusvars
{
	@SuppressWarnings("unchecked")
	/*
	 * returns an Object Array of a passed in Vector, Array, List
	 * (converts to type Object[])
	 *
	 * @param Array Array, List, Vector, or Map
	 * -in case of Map returns an array of the KeySet
	 */
	public static Object[] toArray(Object Arr)
	{
		try {
			if (Arr.getClass().isArray()) {
				return (Object[]) Arr;
			} else if (Arr instanceof Vector) {
				return ((Vector) Arr).toArray();
			} else if (Arr instanceof List) {
				return ((List) Arr).toArray();
			} else if (Arr instanceof Map) {
				return (((Map) Arr).keySet()).toArray();
			} else {
				ortustve.api.DebugLog(ERROR, "ortus_api_toArray(): Unknown Array Type");
				return null;
			}
		} catch (Exception e) {
			ortustve.api.DebugLog(ERROR, "ortus_api_toArray(): Failed!");
			return null;
		}
	}

	public static List GetMapKeysAsList(Map m){
		return new ArrayList(m.keySet());
	}

	public static List MoveElementToIndex(List list, Object element, int index){
		if (list.contains(element)){
			list.remove(element);
			if (index == -1){
				list.add(element);
			} else {
				list.add(index, element);
			}
		}
		return list;
	}

	/*
	 * Adds blank (null) elements to beginning and end of a passed array.
	 * Good for creating blank "filler" items for tables and the like.
	 *
	 * @param Arr - Sage MediaFiles, Airings, Shows or Maps in a Vector, list, map, or Array
	 * @param i1 - the number of blank elements to prepad Arr with. 0 for none.
	 * @param i2 - the number of blank elements to append to Arr. 0 for none.
	 */
	public static Object[] AddArrayElements(Object Arr, int i1, int i2)
	{
		Object[] Arr0 = toArray(Arr);
		int length = Arr0.length;
		Object[] Arr1 = new Object[length + i1 + i2];

		System.arraycopy(Arr0, 0, Arr1, i1, length);

		return Arr1;
	}
	public static List AddListElements(List Arr, int i1, int i2){
		return AddListElements(Arr, i1, i2, "");
	}

//	public static List<String> AddListElements(List<String> Arr, int i1, int i2, String element){
//		return AddListElements(Arr, i1, i2, element);
//	}

	public static List AddListElements(List Arr, int i1, int i2, Object element){
//		System.out.println("AddListElements Begin");
//		System.out.println("Arr = " + Arr);
		if (i1 != 0){
//			System.out.println("i1 != 0, adding elements to the beginning");
			List a1 = Arrays.asList(new Object[i1]);
			Collections.fill(a1, element);
			Arr.addAll(0, a1);
		}
		if (i2 != 0) {
//			System.out.println("i2 != 0, adding elements to the end");
			List a2 = Arrays.asList(new Object[i2]);
//			System.out.println("a2 = " + a2);
			Collections.fill(a2, element);
//			System.out.println("a2 = " + a2);
			Arr.addAll(a2);
		}
		return Arr;
	}
}
